package com.ydl.zhwc.mvpandrxjava.sample.mvp;

import com.ydl.zhwc.mvpandrxjava.bean.Now;
import com.ydl.zhwc.mvpandrxjava.mvp.RxPresenter;
import com.ydl.zhwc.mvpandrxjava.sample.RxUtils;
import com.ydl.zhwc.mvpandrxjava.sample.mvp.MVPContract.OldModel.Callback;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * author : Zhangwenchao
 * e-mail : zhangwch@yidianling.com
 * time   : 2017/12/22
 */

public class MVPPresenter extends RxPresenter<MVPContract.View> implements MVPContract.Presenter {

    MVPContract.OldModel oldModel;

    MVPContract.Model model;

    public MVPPresenter() {
        oldModel = new MVPOldModel();
        model = new MVPModel();
    }


    @Override
    public void loadNow() {
//        loadNow1();
        loadNow2();
    }

    private void loadNow1() {

        oldModel.getNow(new Callback() {
            @Override
            public void onSuccess(Now now) {
                view.updateUI(now);
            }

            @Override
            public void onFail(String msg) {
                view.showMsg(msg);
            }
        });
    }


    private void loadNow2() {
        Disposable disposable = model.getNow()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Now>() {
                    @Override
                    public void accept(Now now) throws Exception {
                        view.updateUI(now);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        view.showMsg(RxUtils.getErrorMsg(throwable));
                    }
                });

        addSubscribe(disposable);
    }


}
